import { IDEGridColumn } from '@ibiz/model-core';
import { GridController } from './grid.controller';

/**
 * 表格列控制器
 * @return {*}
 * @author: zhujiamin
 * @Date: 2022-09-01 18:25:20
 */
export class GridColumnController<T extends IDEGridColumn = IDEGridColumn> {
  /**
   * 表格列模型对象
   *
   * @author lxm
   * @date 2022-09-05 19:09:07
   * @type {T}
   */
  readonly model: T;

  /**
   * 表单控制器
   *
   * @author lxm
   * @date 2022-08-24 22:08:59
   * @type {GridController}
   */
  readonly grid: GridController;

  /**
   * 是否是自适应列
   * @author lxm
   * @date 2023-07-07 11:20:16
   * @type {boolean}
   */
  isAdaptiveColumn: boolean = false;

  /**
   * 上下文
   *
   * @author lxm
   * @date 2022-09-05 19:09:24
   * @readonly
   * @type {IContext}
   */
  get context(): IContext {
    return this.grid.context;
  }

  /**
   * 视图参数
   *
   * @author lxm
   * @date 2022-09-05 19:09:00
   * @readonly
   * @type {IParams}
   */
  get params(): IParams {
    return this.grid.params;
  }

  /**
   * 该列是否启用行编辑
   *
   * @author lxm
   * @date 2022-09-06 11:09:58
   * @readonly
   */
  get enableRowEdit(): boolean {
    return !!(this.grid.model.enableRowEdit && this.model.enableRowEdit);
  }

  /**
   * Creates an instance of GridFieldColumnController.
   * @author lxm
   * @date 2022-08-24 20:08:22
   * @param {T} model
   */
  constructor(model: T, grid: GridController) {
    this.model = model;
    this.grid = grid;
    this.isAdaptiveColumn = model.widthUnit === 'STAR';
    if (this.isAdaptiveColumn) {
      this.grid.hasAdaptiveColumn = true;
    }
  }

  /**
   * 子类不可覆盖或重写此方法，在 init 时需要重写的使用 onInit 方法。
   *
   * @author lxm
   * @date 2022-08-18 22:08:30
   * @returns {*}  {Promise<void>}
   */
  async init(): Promise<void> {
    await this.onInit();
  }

  /**
   * 初始化方法
   *
   * @author lxm
   * @date 2022-09-28 15:09:15
   * @protected
   * @returns {*}  {Promise<void>}
   */
  protected async onInit(): Promise<void> {
    // 初始化操作
  }
}
